package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;

import model.Player;
import model.PlaylistRecord;
import view.content.ImageBox;

public class PlaylistRecordView extends JPanel {
	private PlaylistRecord record;
	private JTable table;

	private static final int DEFAULT_IMAGE_SIZE = 200;


	public PlaylistRecordView(PlaylistRecord rec) {
		this.record = rec;
		this.layoutComponents();
	}

	private void layoutComponents() {
		this.setPreferredSize(new Dimension(410, 410));
		BorderLayout layout = new BorderLayout();
		this.setLayout(layout);
		ImageBox box = new ImageBox(this.record.getContent(), DEFAULT_IMAGE_SIZE, DEFAULT_IMAGE_SIZE);
		box.fit(200, 300);
		box.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
		this.add(box, BorderLayout.CENTER);

		table = new JTable(this.getTableColumns(), new String[] { "field", "value" });
		table.setPreferredSize(new Dimension(200, 400));
		this.add(table, BorderLayout.WEST);

		JButton deleteButton = new JButton("Delete");
		this.add(deleteButton, BorderLayout.SOUTH);

		deleteButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				Player.getInstance().deletePlaylistRecords(new int[] { PlaylistRecordView.this.record.getId() });
			}
		});
	}

	private String[][] getTableColumns() {
		String[][] columns = new String[10][2];
		columns[0][0] = "ID:";
		columns[1][0] = "Pause Time:";
		columns[2][0] = "Content:";
		columns[3][0] = "Time:";
		columns[4][0] = "Playable:";
		columns[5][0] = "Area: ";
		columns[6][0] = "Expired: ";

		columns[0][1] = Integer.toString(this.record.getId());
		columns[1][1] = Integer.toString(this.record.getPauseTime()) + " secs";
		columns[2][1] = this.record.getContent().getPath();
		columns[3][1] = this.record.getRestriction().toString();
		columns[4][1] = Boolean.toString(this.record.isPlayableNow()).toUpperCase();
		columns[5][1] = this.record.getContainer().getName();
		columns[6][1] = Boolean.toString(this.record.isExpired());

		return columns;
	}
}
